package com.net.oil.shop.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.http.custom.BaseObserverNoT;
import com.net.oil.shop.bean.ShopDetailBean;
import com.net.oil.shop.contract.ShopDetailContract;
import com.net.oil.shop.presenter.ShopDetailPresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/4/3:10:01
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class ShopDetailModel extends BaseModel implements ShopDetailContract.Model {

    private ShopDetailPresenter presenter;

    public ShopDetailModel(ShopDetailPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void getShopDetail(int id) {
        AppHttpClient.ApiService().getShopDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ShopDetailBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean<ShopDetailBean> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getShopDetailSuccess(tResultBean.getData());
                        }else {
                            presenter.getShopDetailFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.getShopDetailFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void reserveOrder(int bid, String time, int userCarId) {
        AppHttpClient.ApiService().reserveOrder(bid, time, userCarId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null){
                            presenter.reserveOrderSuccess();
                        }else {
                            presenter.reserveOrderFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.reserveOrderFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });

    }
}
